package dp.路径;

/**
 * todo
 *
 * @author songZiHao
 * @version 1.0.0
 * @ClassName 打家劫舍ii213.java
 * @createTime 2024年03月11日 22:25:00
 */
public class 打家劫舍ii213 {
	public static int rob(int[] nums) {
		int max = Math.max(firstStole(nums), firstNotStole(nums));
		return max;
	}

	public static int firstStole(int[] nums) {
		int length = nums.length;
		int[] dp = new int[length+1];
		dp[0] = 0;
		dp[1] = nums[0];
		for (int i = 2; i <= nums.length - 1; i++) {
			dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1]);
		}
		int max = Integer.MIN_VALUE;
		for (int i : dp) {
			max = Math.max(max, i);
		}
		return max;
	}

	public static int firstNotStole(int[] nums) {
		int length = nums.length;
		int[] dp = new int[length+1];
		dp[0] = 0;
		dp[1] = 0;
		dp[2] = nums[1];
		for (int i = 3; i <= nums.length; i++) {
			dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1]);
		}
		int max = Integer.MIN_VALUE;
		for (int i : dp) {
			max = Math.max(max, i);
		}
		return max;
	}

	public static void main(String[] args) {
		System.out.println(rob(new int[]{1,2,3,1,4}));
	}
}
